package tt.dz.appserver.dao.ext;

import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository;

import tt.dz.appserver.dao.base.BasisDao;
import tt.dz.appserver.entity.model.StationCollection;
import tt.dz.appserver.util.Constants;

/**
 * 用户站点收藏dao
 * 
 * @author Administrator
 *
 */
@Repository
public class StationCollectionDao extends BasisDao<StationCollection> {

	/** 此站点是否已经收藏 */
	private static final String STATIONISCOLLECTION = "SELECT count(*) as count FROM as_station_collection WHERE user_id=? and station_id=?";

	/**
	 * 此站点是否已经收藏
	 * 
	 * @param stationId
	 * @param userId
	 * @return
	 */
	@Cacheable(value="stationColletion",key="#stationId+#userId")
	public boolean stationIsCollection(String stationId, String userId) {

		// 查询个数
		List<Map<String, Object>> counts = super.findDataOfMapBySQL(new StringBuffer(STATIONISCOLLECTION), null, null,
				userId, stationId);

		Map<String, Object> countMap = counts.get(0);
		// 得到个数
		long count = (long) countMap.get("count");

		// 返回是否收藏
		return count > 0 ? true : false;
	}

	/**
	 * 物理删除收藏
	 * @param stationId
	 * @param userId
	 * @return
	 */
	@Caching(evict={
			@CacheEvict(value="stationColletion",key="#stationId+#userId"),
			@CacheEvict(value="stationColletions", allEntries=true)
	})
	public void deleteStationCollection(String stationId, String userId) {

		StationCollection stationCollection = new StationCollection();
		stationCollection.setUser_id(userId);
		stationCollection.setStation_id(stationId);
		super.delete(stationCollection);
	}
	
	@Caching(evict={
			@CacheEvict(value="stationColletion",key="#stationCollection.station_id+#stationCollection.user_id"),
			@CacheEvict(value="stationColletions", allEntries=true)
	})
	public int save(StationCollection stationCollection ) {

		return super.save(stationCollection);
	}
	
	@Caching(cacheable={
			@Cacheable(value="stationColletions",key="#userId+#pageNo")
	})
	public List<StationCollection>  findCollectionStationByUserId(String userId,Integer pageNo){
		
		StationCollection collection = new StationCollection();
		collection.setUser_id(userId);
		return super.findExample(collection, pageNo, Constants.PAGE_SIZE, "coll_time", 0);
		
	}
}
